<template>
  <FullPage>
    <VGrid
      ref="vgrid"
      :formConfig="formConfig"
      :tableConfig="tableConfig"
      :proxy="proxy"
      @handleFormBtnClick="handleFormBtnClick"
      @handleTableBtnClick="handleTableBtnClick"
    ></VGrid>
    <DepartmentFormDialog ref="departmentFormDialog" @submit="getTableData"></DepartmentFormDialog>
  </FullPage>
</template>

<script>
import FullPage from '@/components/FullPage';
import VGrid from '@/components/VGrid';
import DepartmentFormDialog from './DeparmentFormDialog';

export default {
  components: {
    VGrid,
    FullPage,
    DepartmentFormDialog,
  },

  data() {
    return {
      formConfig: {
        props: {
          inline: true,
        },
        btns: [
          {
            key: 'addDepartment',
            label: '新增部门',
            type: 'success',
            icon: 'el-icon-plus',
          },
        ],
        fieldList: [],
      },
      tableConfig: {
        props: {
          border: true,
          stripe: true,
          rowKey: 'id',
        },
        columnList: [
          {
            prop: 'deptName',
            label: '部门名称',
            align: 'left',
          },
          {
            prop: 'phone',
            label: '部门电话',
          },
          {
            prop: 'address',
            label: '部门地址',
          },
          {
            label: '操作',
            type: 'operation',
            btns: [
              {
                key: 'addSubDepartment',
                label: '新增子部门',
              },
              {
                key: 'edit',
                label: '编辑',
              },
              {
                key: 'delete',
                label: '删除',
              },
            ],
          },
        ],
      },
      proxy: {
        list: {
          api: this.$api.account.department.getDepartmentTree,
          resultKey: '',
        },
        delete: {
          api: this.$api.account.department.deleteDept,
        },
      },
    };
  },

  methods: {
    handleFormBtnClick(key, params) {
      if (key === 'addDepartment') {
        this.$refs.departmentFormDialog.show('add');
      }
    },
    handleTableBtnClick(key, row) {
      if (key === 'edit') {
        this.$refs.departmentFormDialog.show('edit', row);
      }

      if (key === 'addSubDepartment') {
        this.$refs.departmentFormDialog.show('add', row);
      }
    },

    getTableData() {
      this.$refs.vgrid.getTableData();
    },
  },
};
</script>

<style lang="scss" scoped></style>
